package com.ljfresh.customer.controller;

import com.alibaba.fastjson.JSONObject;
import com.ljfresh.customer.common.CommonAttribute;
import com.ljfresh.customer.data.LoadEditData;
import com.ljfresh.customer.dto.UDeskResult;
import com.ljfresh.customer.pojo.*;
import com.ljfresh.customer.result.LoadEditResult;
import com.ljfresh.customer.result.OrderDetailsResult;
import com.ljfresh.customer.result.OrderRecordResult;
import com.ljfresh.customer.result.Result;
import com.ljfresh.customer.sercice.OrderTypeService;
import com.ljfresh.customer.sercice.StoreService;
import com.ljfresh.customer.utils.RedisUtils;
import com.ljfresh.customer.utils.udesk.UDeskBasicApiUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Lucifer
 * @time: 2020/4/6-11:04
 */
@Api(value = "工单",tags = "工单接口")
@RestController
@RequestMapping("/workOrder")
public class WorkOrderCoutroller{

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private OrderTypeService orderTypeService;

    @Resource
    private StoreService storeService;

    @GetMapping("/loadEdit")
    @ApiOperation("加载编辑页面数据")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 400,message = "参数错误"),
            @ApiResponse(code = 205003,message = "加载编辑页面失败~")
    })
    public LoadEditResult loadEdit(HttpServletRequest request){
        try {
            List<OrderType> orderTypes = orderTypeService.findAll();
            String sessionId = request.getHeader("sessionId");
            Object o = redisUtils.get(sessionId);
            User user = null;
            if (o!=null){
                user = JSONObject.parseObject(o.toString(),User.class);
            }
            if (user!=null){
                Store store = storeService.find("storeCode",user.getStore());
                return new LoadEditResult(new LoadEditData(orderTypes,store));
            }else{
                return new LoadEditResult(105006);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new LoadEditResult(205003);
        }
    }

    @PostMapping("/save")
    @ApiOperation("提交工单接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 400,message = "参数错误"),
            @ApiResponse(code = 305001,message = "提交工单失败~")
    })
    public Result save(@RequestBody WorkOrder workOrder){
        /*redisUtils.set("bbb","222",1);
        redisUtils.get("bbb");
        Object aaa = redisUtils.get("aaa");
        System.out.println(aaa);*/
        UDeskResult result = UDeskBasicApiUtil.createWorkOrder(workOrder);
        return new Result(305001,result);
    }

    @GetMapping("/findOrderRecord")
    @ApiOperation("查询工单记录接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 400,message = "参数错误")
    })
    public OrderRecordResult findOrderRecord(Date date){
        List<WorkOrder> workOrders = new ArrayList<>();

        return new OrderRecordResult();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("查询工单详情接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 400,message = "参数错误")
    })
    public OrderDetailsResult detail(@PathVariable("id") Long id){

        return new OrderDetailsResult();
    }

}
